<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Refuel extends Base
{
    protected $id = 'refuel_record_id';
	protected $table = 'refuel_record';
	protected $join = ['driver d','d.driver_id = m.driver_id','left'];
	protected $field = 'm.*,d.name';
	
	protected function search() {
		
		$P = input('param.');
		$where = parent::search();
		
		$P['number'] && $where['car_number'] = ['LIKE',"%{$P['number']}%"];
		$P['start_date'] && $where['start_date'] = ['>=',$P['start_date']];

		if ( isset($where['m.number']) ) unset($where['m.number']);
		
//		halt($where);
		return $where;
	}
	
    protected function options($item = []) {

        $labels = config('base.labels');
        $this->assign('labels',TplOptions($labels,$item['label']));

        return $item;

    }
    //下载excel表格
    public function outexcel()
    {

        $P = input('param.');
        $where = [];

        $where['car_number'] = ['LIKE', "%闽%"];
        $P['number'] && $where['car_number'] = ['LIKE', "%{$P['number']}%"];
        $P['start_date'] && $where['start_date'] = ['>=', $P['start_date']];

        $refuel_record_return = Db::name('refuel_record')->where($where)->whereNull('delete_time')->field('car_number,driver_name,label,litre,price,start_date,refuel_add,gas_station,km,ps')->order('start_date desc')->select();

        if ($refuel_record_return) {
            echo "<table border='1'>";
            //标题
            $title = array("车牌号", "加油司机", "油号", "加油量", "加油费用", "加油时间", "加油地点", "加油站", "车辆公里数", "备注说明");
            //文件名
            $filename = '加油记录导出报表';
            header("Content-type:application/octet-stream");
            header("Accept-Ranges:bytes");
            header("Content-type:application/vnd.ms-excel");
            header("Content-Disposition:attachment;filename=" . $filename . ".xls");
            header("Pragma: no-cache");
            header("Expires: 0");
            if (!empty($title)) {
                foreach ($title as $k => $v) {
                    $title[$k] = iconv("UTF-8", "GB2312", "<td>" . $title[$k] . "</td>");
                    echo $title[$k];
                }
                echo "</tr>";
            }
            if (!empty($refuel_record_return)) {
                foreach ($refuel_record_return as $key => $val) {
                    echo "<tr>";
                    foreach ($val as $ck => $cv) {
                        $refuel_record_return[$key][$ck] = iconv("UTF-8", "GB2312", "<td style=\"vnd.ms-excel.numberformat:@\">" . $refuel_record_return[$key][$ck] . "</td>");
                        echo $refuel_record_return[$key][$ck];
                    }
                    echo "</tr>";
                }
            } else {
                $this->error('无数据');
            }
        }
    }
}
